//index.js
var utils = require('../../utils/util.js');

//获取应用实例
const app = getApp();
var pageIndex = 1,
    filterIndex = 1,
    condition = false,
    is_slide = true;
Page({
  data: {
      scroll_h:50,
      list_data:{},
      motto: 'Hello World',
      userInfo: {},
      hasUserInfo: false,
      canIUse: wx.canIUse('button.open-type.getUserInfo'),
      allGoodsFilte: [],
      gnbk: [],
      grade:[],
      slide_is_hide:true,
      is_current:false
  },
    reset_but:function(e){
        console.log(e);
        var allGoodsFilte = this.data.allGoodsFilte,
            gnbk = this.data.gnbk,
            grade = this.data.grade;
        for(var i=0;i<allGoodsFilte.length;i++){
            allGoodsFilte[i].checked = false;
        }
        for(var i=0;i<gnbk.length;i++){
            gnbk[i].checked = false;
        }
        for(var i=0;i<grade.length;i++){
            grade[i].checked = false;
        }
        is_slide = true;
        pageIndex = 1;
        filterIndex = 1;
        this.setData({
            allGoodsFilte:allGoodsFilte,
            gnbk:gnbk,
            grade:grade
        })
    },
    slide_ce:function(){
        /* console.log(111);*/
        if(this.data.slide_is_hide == true){
            var bl = false;
        }else{
            var bl = true;
        }

        this.setData({
            slide_is_hide:bl
        });
    },
    formSubmit:function(e){
        var param = e.detail.value;
        param.pageNo=filterIndex;
        param.orderField=param.orderField.length>0?'score'+param.orderField:'';
        var that =this;
        console.log(param);
        wx.request({
            url: 'https://www.ggd4.cn/wap/projectLibrary/getData',
            data: param,
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function(res) {
                condition = param;
                utils.submit_cg(that,res)
            }
        })
        console.log('form发生了submit事件，携带数据为：', e.detail.value);
    },
    serviceValChange: function (e) {

        this.setData({
            allGoodsFilte: utils.radio('allGoodsFilte',this,e)
        })
    },
    gnbk_fun:function(e){
        this.setData({
            gnbk: utils.radio('gnbk',this,e)
        })
    },
    grade_fun:function(e){
        console.log(utils.radio('grade',this,e));
        this.setData({
            grade: utils.radio('grade',this,e)
        })
    },
    getUserInfo: function(e) {
        console.log(e);
        app.globalData.userInfo = e.detail.userInfo;
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },
    searchScrollLower:function(){
      console.log('到底了');
        wx.showLoading({
            title: '加载中....',
            mask:true
        });
      var that = this;
      if(is_slide){
          if(condition){
              filterIndex++;
              condition.pageNo = filterIndex;
              wx.request({
                  url: 'https://www.ggd4.cn/wap/projectLibrary/getData',
                  data: condition,
                  header: {
                      'content-type': 'application/json' // 默认值
                  },
                  success: function(res) {
                      console.log(res.data);
                      wx.hideLoading();
                      if(res.data.data.length<=0){
                          is_slide = false;
                      }
                      that.setData({
                          list_data:that.data.list_data.concat(res.data.data)
                      })
                  }
              })
          }else{
              pageIndex++;
              wx.request({
                  url: 'https://www.ggd4.cn/wap/ProjectLibrary/getData', //仅为示例，并非真实的接口地址
                  data: {
                      pageNo:pageIndex
                  },
                  header: {
                      'Content-Type': 'application/json;charset=UTF-8;'
                  },
                  method:'GET',
                  success: function(res) {
                      /*that*/
                      console.log(that.data.list_data);
                      console.log(res.data);
                      wx.hideLoading();
                      that.setData({
                          list_data:that.data.list_data.concat(res.data.data)     //累加数据总量
                      })
                  }
              });
          }

      }else{
          wx.showToast({
              title:'没有更多了',
              icon:'none'
          });
      }


    },

  onLoad: function () {
      var that = this;
      utils.init_h(that);
      // console.log(this.data.current_page);
      wx.request({
          url: 'https://www.ggd4.cn/wap/ProjectLibrary/getData', //仅为示例，并非真实的接口地址
          data: {
              pageNo:pageIndex
          },
          header: {
              'Content-Type': 'application/json;charset=UTF-8;'
          },
          method:'GET',
          success: function(res) {
              console.log(res);
              that.setData({
                  list_data:res.data.data
              })
          }
      });
      wx.request({
          url: 'https://www.ggd4.cn/wap/projectLibrary/index',
          header: {
              'Content-Type': 'application/json;charset=UTF-8;'
          },
          method:'GET',
          success: function(res) {
              that.setData({
                  allGoodsFilte:res.data.data.stages,
                  gnbk:res.data.data.concepts,
                  grade:res.data.data.score_categorys
              })
          }
      });
      if (app.globalData.userInfo) {
          console.log(app.globalData.userInfo);
          this.setData({
              userInfo: app.globalData.userInfo,
              hasUserInfo: true
          })
      } else if (this.data.canIUse){
          // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
          // 所以此处加入 callback 以防止这种情况
          app.userInfoReadyCallback = res => {
              this.setData({
                  userInfo: res.userInfo,
                  hasUserInfo: true
              })
          }
      } else {
          // 在没有 open-type=getUserInfo 版本的兼容处理
          wx.getUserInfo({
              success: res => {
                  app.globalData.userInfo = res.userInfo;
                  this.setData({
                      userInfo: res.userInfo,
                      hasUserInfo: true
                  })
              }
          })
      }
  },
    onReady:function(){
      var param = this.data.current_page;
        // console.log(this.data.current_page);
        utils.onready(this)
      /*  if(this.data.current_page.indexOf('xmk')){

        }*/
    }
  // ,
  // getUserInfo: function(e) {
  //   console.log(e)
  //   app.globalData.userInfo = e.detail.userInfo
  //   this.setData({
  //     userInfo: e.detail.userInfo,
  //     hasUserInfo: true
  //   })
  // }
})
